package com.ds.dsm.aggregation.filed;

import com.ds.esd.custom.bean.enums.ModuleViewType;
import com.ds.esd.custom.nav.annotation.TabsAnnotation;
import com.ds.esd.custom.tree.annotation.TreeAnnotation;
import com.ds.esd.dsm.aggregation.AggEntityConfig;
import com.ds.esd.dsm.aggregation.FieldAggConfig;
import com.ds.esd.tool.ui.component.list.TreeListItem;

import java.util.Set;

@TabsAnnotation(singleOpen = true)
@TreeAnnotation(heplBar = true, lazyLoad = true, dynDestory = true)
public class AggFieldTree extends TreeListItem {


    public AggFieldTree(AggEntityConfig entityConfig) {
        this.caption = "字段列表";
        this.imageClass = "spafont spa-icon-c-comboinput";
        this.setEuClassName("dsm.agg.field.config.FieldAggList");
        this.setId(entityConfig.getDomainId() + entityConfig.getESDClass().getClassName());
        this.setIniFold(false);
        this.addTagVar("sourceClassName", entityConfig.getSourceClassName());
        this.addTagVar("entityClassName", entityConfig.getESDClass().getEntityClassName());
        this.addTagVar("domainId", entityConfig.getDomainId());
        this.addTagVar("viewType", ModuleViewType.FormConfig);
        this.addTagVar("entityClassName", entityConfig.getESDClass().getEntityClass().getClassName());
        Set<String> fieldNames = entityConfig.getFieldNames();
        this.setDynDestory(true);
        for (String fieldName : fieldNames) {
            FieldAggConfig fieldFormConfig = entityConfig.getFieldByName(fieldName);
            if (fieldFormConfig != null) {
                fieldFormConfig.setSourceClassName(entityConfig.getSourceClassName());
                fieldFormConfig.setEntityClassName(entityConfig.getESDClass().getCtClass().getName());
                this.addChild(new AggFieldTree(fieldFormConfig));
            }
        }
    }



    public AggFieldTree(FieldAggConfig fieldFormInfo) {
        this.caption = fieldFormInfo.getCaption();
        if (caption == null || caption.equals("")) {
            caption = fieldFormInfo.getFieldname();
        }
        this.setId(fieldFormInfo.getFieldname() + "_" + fieldFormInfo.getSourceClassName() + "_" + fieldFormInfo.getMethodName());
        this.setIniFold(false);

        this.imageClass = fieldFormInfo.getImageClass();
        if (imageClass == null || imageClass.equals("")) {
            if ( fieldFormInfo.getComponentType() != null) {
                imageClass =  fieldFormInfo.getComponentType().getImageClass();
            }
        }

        this.setEuClassName("dsm.agg.field.config.FieldInfo");
        this.addTagVar("sourceClassName", fieldFormInfo.getSourceClassName());
        this.addTagVar("entityClassName", fieldFormInfo.getEntityClassName());
        this.addTagVar("domainId", fieldFormInfo.getDomainId());
        this.addTagVar("fieldname", fieldFormInfo.getFieldname());
    }

}
